package com.hotel.writer.service.impl;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.EnumConstants.ActionType;
import com.hotel.comm.bean.EnumConstants.IndexType;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.company.EjobCompany;
import com.hotel.comm.bean.company.EjobCompanyPosition;
import com.hotel.comm.bean.lucene.IndexableBean;
import com.hotel.comm.bean.lucene.PositionIndexBean;
import com.hotel.comm.service.company.ICompanyPositionService;
import com.hotel.comm.service.company.ICompanyService;
import com.hotel.comm.service.search.ILuceneSearchService;
import com.hotel.comm.service.search.ILuceneWriterService;
import com.hotel.writer.Constants;
import com.hotel.writer.lucene.index.IndexStore;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LuceneWriterServiceImpl implements ILuceneWriterService {
    @Autowired
    private ICompanyPositionService companyPositionService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ILuceneSearchService luceneSearchService;

    @SuppressWarnings("rawtypes")
    @Override
    public IReply save(IndexableBean indexBean) {
        if (indexBean == null) {
            return new DubboReply(Constants.REPLY_CODE_SAVE_INDEXBEANNULL, Constants.REPLY_MSG_SAVE_INDEXBEANNULL);
        }
        if (indexBean.getType().equals(IndexType.INDEX_POSITION)) {
            IndexStore.getPositionIndexStore().put(indexBean);
        } else if (indexBean.getType().equals(IndexType.INDEX_PERSON)) {
            IndexStore.getPersonIndexStore().put(indexBean);
        }
        return new DubboReply();
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public IReply writePosition(Long cityId) {
        try {
            DubboReply<EjobCompanyPosition> reply = (DubboReply<EjobCompanyPosition>) this.companyPositionService.findByCityId(cityId);
            List<EjobCompanyPosition> positions = (List<EjobCompanyPosition>) reply.getCollection();
            if (CollectionUtils.isEmpty(reply.getCollection())) {
                return new DubboReply();
            }
            Map<Long, EjobCompany> companyMap = null;
            for (EjobCompanyPosition position : positions) {
                if (position.getEjobCompany() == null) continue;
                if (companyMap == null) {
                    companyMap = new HashMap<Long, EjobCompany>();
                }
                EjobCompany company = companyMap.get(position.getEjobCompany().getId());
                if (company == null) {
                    company = position.getEjobCompany();
                    companyMap.put(position.getEjobCompany().getId(), company);
                }
                PositionIndexBean _position = new PositionIndexBean(position, company);
                _position.setActionType(ActionType.DELETE);
                IndexStore.getPositionIndexStore().put(_position);
                PositionIndexBean pib = (PositionIndexBean) _position.clone();
                pib.setActionType(ActionType.SAVE);
                IndexStore.getPositionIndexStore().put(pib);
            }
            return new DubboReply();
        } catch (Exception e) {
            e.printStackTrace();
            return new DubboReply(false);
        }
    }

}
